package com.itmk.major.controller;

import com.alibaba.fastjson.JSONObject;
import com.itmk.colloge.entity.Colloge;
import com.itmk.colloge.service.CollogeService;
import com.itmk.colloge.service.impl.CollogeServiceImpl;
import com.itmk.major.entity.Major;
import com.itmk.major.entity.MajorParm;
import com.itmk.major.entity.MajorResult;
import com.itmk.major.service.MajorService;
import com.itmk.major.service.impl.MajorServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.UUID;

///major.do的请求，都会被该servlet处理
@WebServlet("/major.do")
public class MajorServlet extends HttpServlet {
    private static final String PREX = "WEB-INF/jsp/admin";
    CollogeService collogeService = new CollogeServiceImpl();
    private MajorService majorService = new MajorServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (method.equals("majorUI")) {
            majorUI(req, resp);
        } else if (method.equals("addUI")) {
            addUI(req, resp); //新增页面
        } else if (method.equals("addSave")) {
            addSave(req, resp); //新增保存
        } else if (method.equals("editUI")) {
            editUI(req, resp);//编辑页面
        } else if (method.equals("editSave")) {
            editSave(req, resp);
        } else if (method.equals("lsit")) {
            list(req, resp);
        } else if (method.equals("delete")) {
            delete(req, resp);
        }else if(method.equals("majorCount")){
            majorCount(req, resp);
        }
    }

    private void majorCount(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        MajorResult majorResult = majorService.majorCount();
        //响应前端
        resp.setContentType("application/json;charset=UTF-8");
        JSONObject object = new JSONObject();
        object.put("code",200);
        object.put("msg","查询成功");
        object.put("data",majorResult);
        //写回前端
        PrintWriter writer = resp.getWriter();
        writer.print(object.toJSONString());
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取前端传递过来的id
        String majorId = req.getParameter("majorId");
        majorService.delete(majorId);
        //响应前端
        resp.setContentType("application/json;charset=UTF-8");
        JSONObject object = new JSONObject();
        object.put("code",200);
        object.put("msg","删除成功");
        //写回前端
        PrintWriter writer = resp.getWriter();
        writer.print(object.toJSONString());
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        MajorParm parm = new MajorParm();
        //获取前端传递过来的参数
        String majorName = req.getParameter("majorName");
        int page = Integer.valueOf(req.getParameter("page"));
        int limit = Integer.valueOf(req.getParameter("limit"));
        parm.setLimit(limit);
        parm.setMajorName(majorName);
        parm.setPage(page);
        //查询list
        List<Major> majorList = majorService.list(parm);
        //查询总条数
        int count = majorService.getCount(parm);
        //构造前端表格需要的数据格式
        JSONObject object = new JSONObject();
        object.put("code", 0);
        object.put("msg", "查询成功");
        object.put("data", majorList);
        object.put("count", count);
        //写给前端
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.print(object.toJSONString());

    }


    private void editUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、查询所有的学院
        List<Colloge> collogeList = collogeService.getAll();
        //放到request对象
        req.setAttribute("editCollogeList", collogeList);
        //2、查询要编辑的数据
        //获取编辑数据的id
        String majorId =req.getParameter("majorId");
        Major major = majorService.getById(majorId);
        req.setAttribute("editMajor", major);
        //3、返回页面
        req.getRequestDispatcher(PREX + "/major_edit.jsp").forward(req, resp);
    }

    /**
     * 新增保存
     *
     * @param req
     * @param resp
     */
    private void addSave(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取前端传递的参数
        String collogeId = req.getParameter("collogeId");
        int orderNum = Integer.valueOf(req.getParameter("orderNum"));
        String majorName = req.getParameter("majorName");
        //封装参数
        Major major = new Major();
        major.setMajorId(UUID.randomUUID().toString().replaceAll("-",""));
        major.setCollogeId(collogeId);
        major.setMajorName(majorName);
        major.setOrderNum(orderNum);
        //调用service层方法
        majorService.add(major);
        //返回数据告诉前端，新增成功
        resp.setContentType("application/json;charset=UTF-8");
        JSONObject object = new JSONObject();
        object.put("code", 200);
        object.put("msg", "新增成功!");
        //写回前端
        PrintWriter writer = resp.getWriter();
        writer.print(object.toJSONString());

    }

    private void editSave(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取前端传递过来的参数
        String majorId = req.getParameter("majorId");
        String collogeId = req.getParameter("collogeId");
        int orderNum = Integer.valueOf(req.getParameter("orderNum"));
        String majorName = req.getParameter("majorName");
        //组装数据
        Major major = new Major();
        major.setMajorId(majorId);
        major.setMajorName(majorName);
        major.setCollogeId(collogeId);
        major.setOrderNum(orderNum);
        //调用service
        majorService.updateById(major);
        //返回数据告诉前端，新增成功
        resp.setContentType("application/json;charset=UTF-8");
        JSONObject object = new JSONObject();
        object.put("code", 200);
        object.put("msg", "编辑成功!");
        //写回前端
        PrintWriter writer = resp.getWriter();
        writer.print(object.toJSONString());
    }

    /**
     * 返回新增专业页面
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void addUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查询所有的学院
        List<Colloge> collogeList = collogeService.getAll();
        //放到request对象
        req.setAttribute("collogeList", collogeList);
        req.getRequestDispatcher(PREX + "/major_add.jsp").forward(req, resp);
    }

    /**
     * 返回专业管理页面
     *
     * @param req
     * @param resp
     */
    private void majorUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher(PREX + "/major.jsp").forward(req, resp);
    }
}
